package br.com.agenda.support.comp;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.log4j.Logger;
import org.primefaces.event.SelectEvent;
import org.springframework.context.annotation.Scope;
import org.springframework.web.context.WebApplicationContext;

import br.com.agenda.model.CompEntity;
import br.com.agenda.model.repositories.ICompRepository;
import br.com.agenda.model.utils.BaseBeans;

@Scope(value = WebApplicationContext.SCOPE_SESSION)
@Named(value = "compMB")
public class CompMB extends BaseBeans {

	private static final long serialVersionUID = 1L;
	
	private static final Logger logger = Logger.getLogger(CompMB.class);
	
	@Inject
	private ICompRepository compRepository;
	
	private List<CompEntity> comps;
	
	private CompEntity selectedComp;
	
	private Long id;
	
	public CompMB(){
		
	}
	
	public void onLoad(){
		this.comps = this.compRepository.findAll();
	}
	
	public List<CompEntity> getComps(){
		return this.comps;
	}	
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void delete(){
		if(this.selectedComp != null){
			this.compRepository.delete(this.selectedComp.getId());
		}
	}
	
	public void selectComp(SelectEvent evt){
		try{
			if(evt.getObject() != null){
				this.selectedComp = (CompEntity) evt.getObject();
			}else{
				this.selectedComp = null;
			}
		}catch(Exception e){
			this.selectedComp = null;
			
			logger.error(e.getMessage(), e);
		}
	}
	
	public void unselectComp(){
		this.selectedComp = null;
	}
	
	public CompEntity getSelectedComp(){
		return this.selectedComp;
	}
	
	public void setSelectedComp(CompEntity selectedComp){
		this.selectedComp = selectedComp;
	}
}
